﻿package com.petermacko.text.effects {
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	//using: TypeWriter.writeIt(inputText, textFieldPath, speed);
	public class TypeWriter
    {
		public var tt:Timer;
		private var ta:Array = new Array();
		private var t1:String;
		private var c:Number = 0;
		private var tp:Object;
		private var r:Boolean = true;
		
		public function TypeWriter() {
		}
		//_it: inputText, _tp: textPath, _ai: appendInt
		public function writeIt(_it:String, _tp:Object, _ai:Number) {
			if(r == true) {
				tp = _tp;
				ta = [];
				ta.length = 0;
				t1 = _it;
				ta = t1.split("");
				c = 0;
				tp.text = "";
				tt = new Timer(_ai, ta.length);
				tt.addEventListener(TimerEvent.TIMER, appendText);
				tt.addEventListener(TimerEvent.TIMER_COMPLETE, ttStop);
				tt.start();
				r = false;
			}
		}
		private function appendText(e:TimerEvent):void {
			tp.appendText(ta[c]);
			c++;
		}
		private function ttStop(e:TimerEvent):void {
			r = true;
		}
    }
}